<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");

$errMsg = "";
$process_unit = sxREQ('pu');
if($process_unit == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'pu'"));
	serverRedir($url);
}
$entry_point = sxREQ('ep');
if($entry_point == ""){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing cod. 'ep'"));
	serverRedir($url);
}

if(sxREQ("reqtype") == "save"){
	$errMsg .= entpMod();
}
$sSQL = "SELECT * FROM entry_point " .
		"WHERE process_unit='".sxRAS('pu')."' " .
		"AND entry_point=".sxRInt("ep")."";
$objSDAO->query($sSQL,"selep");
$row = $objSDAO->fetch("selep");
if(!$row){	
	$url = $_SESSION[USEROBJ]->getHistory(0, true).
		"&msg=".urlencode(_t("missing  entry point $process_unit/$entry_point"));
	serverRedir($url);
}
sxRDef('new_code', $row['entry_point']);
sxRDef('description', $row['description']);
sxRDef('user_range', $row['user_range']);
sxRDef('note', $row['note']);

$aOpz = array("kMenu"=>'pro.pru', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>
  
<?php
echo(bodyHTML($aOpz));

echo getPuHeader($process_unit);

$aCmd = array("save"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sCmd = commandsHTML($aCmd);	
echo $sCmd;

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('pu');
echo getHtmlHidden('ep');

echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Entry point</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Code*</td>" .
			"<td class=\"text01\">" .
			getHtmlText('new_code',4,4)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description*</td>" .
			"<td class=\"text01\">".getHtmlText('description',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">User range</td>" .
			"<td class=\"text01\">".getHtmlText('user_range',80,80)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\">".getHtmlTextArea('note',80,5)."</td>" .
		"</tr>\n");
echo("</table>");
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function entpMod(){
	global $objSDAO;
	$errMsg = "";
	if(sxRTrim("description")=='')
		$errMsg .= "missing description<br>";
	if(preg_match("/[^0-9]/",sxREQ("new_code")))
		$errMsg .= "not numeric or > 9999 code<br>";
	if($errMsg != "")
		return $errMsg;
	//existence
	if (sxRInt('ep')!=sxRInt('new_code')) {
		$sSQL = "SELECT entry_point FROM entry_point " .
				"WHERE process_unit='".sxRAS('pu')."' " .
				"AND entry_point=".sxRInt("new_code")."";
		$objSDAO->query($sSQL,"test");
		$row = $objSDAO->fetch("test");
		if($row)
			$errMsg .= 'existing code<br>';
	}
	if($errMsg != "")
		return $errMsg;
		
	$sSQL = "UPDATE entry_point " .
				"SET description='".sxRAS("description")."', " .
				"note='".sxRAS("note")."', " .
				"user_range='".sxRAS("user_range")."'" .
				"WHERE process_unit='".sxRAS("pu")."' AND " .
				"entry_point=".intval(sxREQUEST("ep"));	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);
	return 'redirect error';
}
